package datapack

import (
	"broker/interface/imodel"
	"broker/struct/model"
	"encoding/binary"
)

/**
 * @Description
 * @Author huan.wang
 * @Date 2021/4/30 11:02 上午
 **/

type UnSubscribeDatapack struct {
}

func NewUnSubscribeDatapack() *UnSubscribeDatapack {
	return &UnSubscribeDatapack{}
}

func (u UnSubscribeDatapack) Pack(message imodel.IMessage) ([]byte, error) {
	panic("implement me")
}

func (u UnSubscribeDatapack) UnPack(bytes []byte) (imodel.IMessage, error) {
	unSubMsg := model.NewUnSubscribeMessage()
	pdp := NewParamDataPack(bytes)
	paramData, err := pdp.UnPackByLen(2)
	if err != nil {
		return nil, err
	}
	binary.Read(paramData.GetDataBuffer(), binary.BigEndian, &unSubMsg.PacketID)
	for {
		filterData, err := pdp.UnPack()
		if err != nil {
			break
		}
		topicFilter := model.NewTopicFilter(string(filterData.GetData()), 0)
		unSubMsg.AddTopicFilter(topicFilter)
	}
	return unSubMsg, nil
}
